//
//  SSCAdsUnityAdapter.m
//  Unity-iPhone
//
//  Created by xuzepei on 16/4/12.
//
//

#import "SSCAnalyticsUnityAdapter.h"

@implementation SSCAnalyticsUnityAdapter

+ (SSCAnalyticsUnityAdapter*)getInstance
{
    static SSCAnalyticsUnityAdapter* sharedInstance = nil;
    static dispatch_once_t token;
    
    dispatch_once(&token, ^{
        sharedInstance = [[SSCAnalyticsUnityAdapter alloc] init];
        [GAI sharedInstance].logger.logLevel = kGAILogLevelVerbose;
    });
    
    return sharedInstance;
}

- (void)startSession:(int)types
{
    [[SSCAnalytics getInstance] startSession:types];
}

- (void)startAllSessions
{
    [[SSCAnalytics getInstance] startAllSessions];
}

- (void)endSession;
{
    [[SSCAnalytics getInstance] endSession];
}

- (void)sendEvent:(char*)category action:(char*)action label:(char*)label value:(NSNumber*)value params:(NSDictionary*)params
{
    NSString* param0 = nil;
    NSString* param1 = nil;
    NSString* param2 = nil;
    
    if(category && strlen(category))
    {
        param0 = [NSString stringWithFormat:@"%s",category];
    }
    
    if(action && strlen(action))
    {
        param1 = [NSString stringWithFormat:@"%s",action];
    }
    
    if(label && strlen(label))
    {
        param2 = [NSString stringWithFormat:@"%s",label];
    }
    
    if(0 == [param0 length])
        param0 = @"UserEvent";
    
    [[SSCAnalytics getInstance] sendEvent:param0 action:param1 label:param2 value:value params:params];
}

- (void)sendScreenEvent:(char*)screenName
{
    NSString* param0 = nil;
    if(screenName && strlen(screenName))
    {
        param0 = [NSString stringWithFormat:@"%s",screenName];
        [[SSCAnalytics getInstance] sendEventScreen:param0];
    }
}

- (void)setDebugMode:(BOOL)isDebug
{
    [[SSCAnalytics getInstance] setDebugMode:isDebug];
}

- (BOOL)getDebugMode
{
    return [[SSCAnalytics getInstance] getDebugMode];
}

@end

// When native code plugin is implemented in .mm / .cpp file, then functions
// should be surrounded with extern "C" block to conform C function naming rules
extern "C" {
    
    /*To start session by type
     * @param types: int  the values base on the enumeration definition below.
     * typedef enum AnalyticsType
     *{
     * AT_UNKNOWN = 0,
     * AT_FLURRY = 1,
     * AT_NATIVE = 2,
     * AT_GOOGLE = 4,
     * AT_QUANTCAST = 8,
     * };
     */
    void _StartSession(int types)
    {
        [[SSCAnalyticsUnityAdapter getInstance] startSession:types];
    }
    
    //To start all sessions
    void _StartAllSessions()
    {
        [[SSCAnalyticsUnityAdapter getInstance] startAllSessions];
    }
    
    //To end session
    void _EndSession()
    {
        [[SSCAnalyticsUnityAdapter getInstance] endSession];
    }
    
    /* To send event
     * @param category: char* the category of event
     * @param action: char* the action of event, usually it equals to name of event
     * @param label: char* the label of event
     * @param value: long the value of event
     */
    void _SendEvent(char* category, char* action, char* label, long value)
    {
        [[SSCAnalyticsUnityAdapter getInstance] sendEvent:category action:action label:label value:[NSNumber numberWithLong:value] params:nil];
    }
    
    //To send screen event
    void _SendScreenEvent(char* screenName)
    {
        [[SSCAnalyticsUnityAdapter getInstance] sendScreenEvent:screenName];
    }
    
    /* To set debug mode
     * Note: The default debug mode is true which means it will not send data to server.
     */
    void _SetDebugMode(bool isDebugMode)
    {
        [[SSCAnalyticsUnityAdapter getInstance] setDebugMode:isDebugMode];
    }
    
    //To get debug mode
    bool _GetDebugMode()
    {
        return [[SSCAnalyticsUnityAdapter getInstance] getDebugMode];
    }
}
